<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE HTML>
<html>
<head>
<c:import url="/includes/header.jsp"></c:import>
<c:import url="/includes/modal.jsp"></c:import>
</head>
<body>
	<div class="container" id="pager2">
		<form id="listForm" method="post"
			action="${pageContext.request.contextPath}/admin/role/getRole.do">
			<div class="row">
				<div class="col-md-4" style="padding: 0; margin-bottom: 5px;">
					<a class="btn btn-primary"
						href="${pageContext.request.contextPath}/admin/role/addRole.do">+
						新建角色</a>
				</div>
				<div class="col-md-4"></div>
			</div>
			<div class="row">
				<table class="tab table table-striped table-bordered">
					<tr>
						<th>角色</th>
						<th>描述</th>
						<th>创建时间</th>
						<th width="25%">操作</th>
					</tr>
					<c:forEach items="${roles}" var="a">
						<tr>
							<td>${a.name}</td>
							<td>${a.remark}</td>
							<td><fmt:formatDate value="${a.createAt}"
									pattern="yyyy-MM-dd HH:mm:ss" /></td>
							<td><a class="btn btn-default btn-sm btn-edit" data-id="${a.id}">编辑</a>&nbsp;&nbsp;&nbsp;
								<a class="btn btn-default btn-sm btn-del" data-id="${a.id}">删除</a>
							</td>
						</tr>
					</c:forEach>
				</table>
				<c:import url="/includes/page.jsp"></c:import>
			</div>
		</form>
	</div>
</body>
</html>
<script type="text/javascript">
	$(function() {

		$(".table").find("button[data-toggle='modal']").click(function() {

			var id = $(this).attr('data-id');
			var url = $(this).attr('data-href');
			url += '?id=' + id + '&t=' + Math.random(1000);
			$('#myModal').modal({
				remote : url
			})

		});

		//删除 
		$(".btn-del")
				.click(
						function() {
							var flag = confirm("确定删除？");
							var id = $(this).attr("data-id");
							var url = "${pageContext.request.contextPath}/admin/role/delRole.do?id="
									+ id;
							if (flag == true) {
								var result = 0;
								$.ajax({
									type : "post",
									dataType : "json",
									url : url,
									async : false,
									success : function(data) {
										if (data.code == "0") {
											bootbox.alert("删除成功!");
											result = 1;
										} else {
											bootbox.alert("网络不稳定,运行失败");
										}
									},
									error : function() {
										bootbox.alert("网络不稳定,运行失败");
									}
								});
								if (result == 1) {
									$(this).parent().parent().hide(1000);
								}
							}
						});

		//编辑
		$(".btn-edit")
				.click(
						function() {
							var id = $(this).attr("data-id");
							var url = "${pageContext.request.contextPath}/admin/role/editRole.do?id="
									+ id;
							window.location = url;
						});

	});
</script>
